/*
 * @lc app=leetcode.cn id=155 lang=javascript
 *
 * [155] 最小栈
 */

// @lc code=start

var MinStack = function() {
  this.items = [];
  this.mins = [];
};

MinStack.prototype.itemSize = function() {
  return this.items.length;
}

MinStack.prototype.isItemEmpty = function() {
  return this.itemSize() === 0;
}

MinStack.prototype.minSize = function() {
  return this.mins.length;
}

MinStack.prototype.isMinEmpty = function() {
  return this.minSize() === 0;
}

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function(val) {
  this.items.push(val);

  if (this.isMinEmpty() || val <= this.minTop()) {
    this.mins.push(val);
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
  let item = this.items.pop();

  if (item === this.minTop()) {
    this.mins.pop();
  }
  return item;
};

/**
 * @return {number}
 */
MinStack.prototype.top = function() {
  if (this.isItemEmpty()) {
    return undefined;
  }

  return this.items[this.itemSize() - 1];
};

MinStack.prototype.minTop = function() {
  if (this.isMinEmpty()) {
    return undefined;
  }

  return this.mins[this.minSize() - 1];
}

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
  return this.minTop();
};

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */
// @lc code=end

// const minStack = new MinStack();
// minStack.push(-2);
// minStack.push(0);
// minStack.push(-3);
// console.log('minStack.getMin()', minStack.getMin());
// console.log('minStack.pop()', minStack.pop());
// console.log('minStack.top()', minStack.top());
// console.log('minStack.getMin()', minStack.getMin());
